package com.ihg.hiex.admin.dao.session;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.before.test.PreWorkAssignment;
import com.ihg.hiex.entity.session.participator.Type;
import com.ihg.hiex.entity.session.task.SessionTask;
import com.ihg.hiex.entity.session.task.Status;
import com.ihg.hiex.entity.user.Assessor;
import com.ihg.hiex.entity.user.Delegate;

/**
 *
 * @author Ark
 *
 */
@Dao("hibernate")
public interface SessionTaskDao {
    
    /**
     * 
     * @param session
     * @param name
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND name = :name")
    public List<SessionTask> list(@Variable("session") Session session, @Variable("name") String name);
    
    
    /**
     * 
     * @param session
     * @param assessor
     * @param parent
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = session AND assessor = :assessor AND parent = :parent")
    public List<SessionTask> listChildTask(@Variable("session") Session session, @Variable("assessor") Assessor assessor, @Variable("parent") String parent);
    
    /**
     * @param session
     * @param names
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND name IN (:names)")
    public List<SessionTask> list(@Variable("session") Session session, @Variable("names") List<String> names);
    
    /**
     * @param session
     * @param assessor
     * @param name
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND assessor = :assessor AND name = :name")
    public List<SessionTask> list(@Variable("session") Session session, @Variable("assessor") Assessor assessor, @Variable("name") String name);
    
    /**
     * @param session
     * @param delegate
     * @param name
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND delegate = :delegate AND name = :name")
    public List<SessionTask> list(@Variable("session") Session session, @Variable("delegate") Delegate delegate, @Variable("name") String name);
    
    /**
     * @param session
     * @param parent
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND parent = :parent")
    public List<SessionTask> listByParent(@Variable("session") Session session, @Variable("parent") String parent);
    
    /**
     * 
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND (parent = :parent or name = :parent)")
    public List<SessionTask> listParentAndChild(@Variable("session") Session session, @Variable("parent") String parent);
    
    /**
     * @param session
     * @param parent
     * @param assessor
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND parent = :parent AND assessor = :assessor")
    public List<SessionTask> listByParent(@Variable("session") Session session, @Variable("parent") String parent, @Variable("assessor") Assessor assessor);
    
    /**
     * @param session
     * @param parent
     * @param delegate
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND parent = :parent AND delegate = :delegate")
    public List<SessionTask> listByParent(@Variable("session") Session session, @Variable("parent") String parent, @Variable("delegate") Delegate delegate);
    
    /**
     * 
     * @param session
     * @param assessorIds
     * @param names
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND name in (:names) AND assessor.id in (:assessorIds)")
    public List<SessionTask> list(@Variable("session") Session session, @Variable("assessorIds") Long[] assessorIds, @Variable("names") String[] names);
    
    /**
     * 
     * @param session
     * @param parents
     * @param target
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND parent in (:parents) AND target = :target")
    public List<SessionTask> list(@Variable("session") Session session, @Variable("parents") String[] parents, @Variable("target") Type target);
    
    /**
     * 
     * @param session
     * @param names
     * @param target
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND name in (:names) AND target = :target")
    public List<SessionTask> listByName(@Variable("session") Session session, @Variable("names") String[] names, @Variable("target") Type target);
    
    /**
     * @param session
     * @param names
     * @param target
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND name in (:names) AND target = :target")
    public List<SessionTask> listByName(@Variable("session") Session session, @Variable("names") List<String> names, @Variable("target") Type target);
    
    /**
     * 
     * @param session
     * @param delegate
     * @param startDate
     * @param endDate
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND target = 'DELEGATE' AND delegate = :delegate AND startDate >= :startDate AND startDate <= :endDate")
    public List<SessionTask> list(@Variable("session") Session session, @Variable("delegate") Delegate delegate, @Variable("startDate") Date startDate, @Variable("endDate") Date endDate);
    
    /**
     * 
     * @param session
     * @param target
     * @param names
     * @param parent
     * @param status
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND target = :target AND name in (:names) AND parent = :parent AND status != :status")
    public List<SessionTask> listUnfinishTask(@Variable("session") Session session, @Variable("target") Type target, @Variable("names") String[] names, @Variable("parent") String parent, @Variable("status") Status status);
    
    /**
     * 
     * @param session
     * @param target
     * @param delegate
     * @param parent
     * @param status
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND target = :target AND delegate = :delegate AND parent = :parent AND status != :status")
    public List<SessionTask> listUnfinishTask(@Variable("session") Session session, @Variable("target") Type target, @Variable("delegate") Delegate delegate, @Variable("parent") String parent, @Variable("status") Status status);
    
    /**
     * 
     * @param session
     * @param delegates
     * @param date
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND target = 'DELEGATE' AND delegate in(:delegates) AND startDate <= :date AND endDate >= :date AND parent IS NULL")
    public List<SessionTask> listMainTaskByDelegate(@Variable("session") Session session, @Variable("delegates") List<Delegate> delegates, @Variable("date") Date date);
    
    /**
     * 
     * @param session
     * @param delegate
     * @param parent
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND target = 'DELEGATE' AND delegate = :delegate AND parent = :parent")
    public List<SessionTask> listItemTaskByDelegate(@Variable("session") Session session, @Variable("delegate") Delegate delegate, @Variable("parent") String parent);
    
    /**
     * 
     * @param session
     * @param assessor
     * @param startDate
     * @param endDate
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND target = 'ASSESSOR' AND assessor = :assessor AND startDate >= :startDate AND startDate <= :endDate")
    public List<SessionTask> list(@Variable("session") Session session, @Variable("assessor") Assessor assessor, @Variable("startDate") Date startDate, @Variable("endDate") Date endDate);
    
    /**
     * 
     * @param session
     * @param assessors
     * @param date
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND target = 'ASSESSOR' AND assessor in(:assessors) AND startDate <= :date AND endDate >= :date AND parent IS NULL")
    public List<SessionTask> listMainTaskByAssessor(@Variable("session") Session session, @Variable("assessors") List<Assessor> assessors, @Variable("date") Date date);
    
    /**
     * 
     * @param session
     * @param assessor
     * @param delegate
     * @param parent
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND target = 'ASSESSOR' AND assessor = :assessor AND delegate = :delegate AND parent = :parent")
    public List<SessionTask> listItemTaskByAssessor(@Variable("session") Session session, @Variable("assessor") Assessor assessor, @Variable("delegate") Delegate delegate, @Variable("parent") String parent);
    
    /**
     * 
     * @param session
     * @param startDate
     * @param endDate
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND target = 'ADMINISTRATOR' AND startDate >= :startDate AND startDate <= :endDate")
    public List<SessionTask> listAdmin(@Variable("session") Session session, @Variable("startDate") Date startDate, @Variable("endDate") Date endDate);
    
    /**
     * 
     * @param session
     * @param assessor
     * @param delegate
     * @param parent
     * @param target
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND assessor = :assessor AND delegate = :delegate AND parent = :parent AND target = :target")
    public List<SessionTask> list(@Variable("session") Session session, @Variable("assessor") Assessor assessor, @Variable("delegate") Delegate delegate, @Variable("parent") String parent, @Variable("target") Type target);
    
    /**
     * 
     * @param session
     * @param assessor
     * @param delegate
     * @param target
     * @param parent
     * @param status
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND assessor = :assessor AND delegate = :delegate AND target = :target AND parent = :parent AND status != :status")
    public List<SessionTask> list(@Variable("session") Session session, @Variable("assessor") Assessor assessor, @Variable("delegate") Delegate delegate, @Variable("target") Type target, @Variable("parent") String parent, @Variable("status") Status status);

    /**
     * @param session
     * @param name
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND name = :name")
    public SessionTask load(@Variable("session") Session session, @Variable("name") String name);
    
    /**
     * @param session
     * @param assessor
     * @param parent
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND assessor = :assessor AND parent = :parent")
    public SessionTask loadByParent(@Variable("session") Session session, @Variable("assessor") Assessor assessor, @Variable("parent") String parent);
    
    /**
     * @param session
     * @param name
     * @param target
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND name = :name AND target = :target")
    public SessionTask load(@Variable("session") Session session, @Variable("name") String name, @Variable("target") Type target);
    
    /**
     * @param session
     * @param assessor
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND assessor = :assessor")
    public SessionTask load(@Variable("session") Session session, @Variable("assessor") Assessor assessor);
    
    /**
     * @param session
     * @param assessor
     * @param name
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND assessor = :assessor AND name = :name")
    public SessionTask load(@Variable("session") Session session, @Variable("assessor") Assessor assessor, @Variable("name") String name);
    
    /**
     * @param session
     * @param delegate
     * @param name
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND delegate = :delegate AND name = :name")
    public SessionTask load(@Variable("session") Session session, @Variable("delegate") Delegate delegate, @Variable("name") String name);
    
    /**
     * 
     * @param session
     * @param assessor
     * @param delegate
     * @param target
     * @param name
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND assessor = :assessor AND delegate = :delegate AND target = :target AND name = :name")
    public SessionTask load(@Variable("session") Session session, @Variable("assessor") Assessor assessor, @Variable("delegate") Delegate delegate, @Variable("target") Type target, @Variable("name") String name);

    /**
     * 
     * @param session
     * @param delegate
     * @return
     */
    @Query("FROM PreWorkAssignment WHERE disabled = false AND session = :session AND delegate = :delegate")
    public PreWorkAssignment load(@Variable("session") Session session, @Variable("delegate") Delegate delegate);
    
    /**
     * 
     * @param session
     * @param delegate
     * @param startDate
     * @param endDate
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND target = 'DELEGATE' AND delegate = :delegate AND startDate >= :startDate AND startDate <= :endDate AND parent IS NULL")
    public SessionTask load(@Variable("session") Session session, @Variable("delegate") Delegate delegate, @Variable("startDate") Date startDate, @Variable("endDate") Date endDate);
    
    /**
     * 
     * @param session
     * @param assessor
     * @param startDate
     * @param endDate
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND target = 'ASSESSOR' AND assessor = :assessor AND startDate >= :startDate AND startDate <= :endDate AND parent IS NULL")
    public SessionTask load(@Variable("session") Session session, @Variable("assessor") Assessor assessor, @Variable("startDate") Date startDate, @Variable("endDate") Date endDate);
    
    
}
